import asyncio
import time

from api_utils.base_api import BaseApi, run_api

semaphore = asyncio.Semaphore(10)


class BaseDate(BaseApi):

    def __init__(self):
        super().__init__()

    async def update_token(self):
        api = await run_api()
        self.access_token = api.access_token
        self.refresh_token = api.refresh_token


class StockData:
    def __init__(self, base_data):
        self.base_data = base_data

    async def get_fba_stock_source(self, sid):
        """ 从api接口导出后台源表FBA库存数据

        Args:
            sid: 店铺id
        """
        await self.base_data.update_token()
        req_body = {
            "sid": sid,
            "offset": 0,
            "length": 1000
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/erp/sc/data/mws_report/manageInventory", "POST", req_body=req_body)
        return result

    async def get_fba_stock_lingxing(self, sid):
        """ 从api接口导出领星FBA库存数据

        Args:
            sid: 店铺id
        """
        await self.base_data.update_token()
        req_body = {
            "sid": sid,
            "offset": 0,
            "length": 1000
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/erp/sc/routing/fba/fbaStock/fbaList", "POST", req_body=req_body)
        return result
    
    async def get_fba_stock_lingxing_v2(self):
        """ 从api接口导出领星FBA库存数据 v2
        """
        await self.base_data.update_token()
        req_body = {
            "cid": "",
            "bid": "",
            "attribute": "",
            "asin_principal": "",
            "search_field": "asin",
            "search_value": "B0B6ZH72G4",
            "is_cost_page": "",
            "status": "",
            "senior_search_list": "",
            "offset": 0,
            "length": 20,
            "fulfillment_channel_type": "",
            "is_hide_zero_stock": "",
            "is_parant_asin_merge": "",
            "is_contain_del_ls": ""
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/basicOpen/openapi/storage/fbaWarehouseDetail", "POST", req_body=req_body)
        return result


    async def get_yuliu_stock_source(self, sid):
        """ 从api接口导出后台源表预留库存数据

        Args:
            sid: 店铺id
        """
        await self.base_data.update_token()
        req_body = {
            "sid": sid,
            "offset": 0,
            "length": 1000
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/erp/sc/data/mws_report/reservedInventory", "POST", req_body=req_body)
        return result
    
    async def get_awd_stock_lingxing(self, storehouse_id:str):
        """从api接口导出领星AWD库存数据

        Args:
            storehouse_id: 仓库id表，字符串使用逗号分隔
        """
        await self.base_data.update_token()
        req_body = {
                "wids": storehouse_id,
                "cid": "",
                "bid": "",
                "attribute": "",
                "asin_principal": "",
                "search_field": "sku",
                "search_value": "",
                "status": "",
                "is_hide_zero_stock": 1,
                "offset": 0,
                "length": 200
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/basicOpen/openapi/storage/awdWarehouseDetail", "POST", req_body=req_body)
        return result

    async def get_storehouse_id(self):
        """ 从api接口导出仓库id
        """
        await self.base_data.update_token()
        req_body = {
            "type": 6,  # 6表示AWD仓
            "is_delete": 0,
            "offset": 0,
            "length": 1000
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/erp/sc/data/local_inventory/warehouse", "POST", req_body=req_body)
        return result
    
    async def get_fba_shipment(self,sid,start_date,end_date):
        """从api接口导出FBA货件信息

        Args:
            sid: 店铺id
        """
        await self.base_data.update_token()
        req_body = {
            "end_date": end_date,
            "offset": 0,
            "length": 1000,
            "sid": sid,
            "start_date": start_date
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/erp/sc/data/fba_report/shipmentList", "POST", req_body=req_body)
        return result


class SalesData:
    def __init__(self, base_data):
        self.base_data = base_data

    async def get_30day_sales(self,sid,start_date,end_date):
        """ 从api接口导出店铺销量数据

        Args:
            sid: 店铺id
            start_date: 开始日期(2024-08-05)
            end_date: 结束日期
        """
        await self.base_data.update_token()
        req_body = {
            "sid": sid,
            "start_date": start_date,
            "end_date": end_date,
            "offset": 0,
            "length": 1000
        }
        result = await self.base_data.op_api.request(self.base_data.access_token, "/erp/sc/data/sales_report/sales", "POST", req_body=req_body)
        return result

